package top.tupobi.zing.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;

import java.util.LinkedList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import top.tupobi.zing.InfoRecord;
import top.tupobi.zing.R;
import top.tupobi.zing.mvp.model.FriendModel;
import top.tupobi.zing.mvp.model.TbMessage;
import top.tupobi.zing.network.HttpConfig;

public class AdapterRvChat extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_LEFT = 5454;
    private static final int TYPE_RIGHT = 5455;
    private static final int TYPE_TIME = 5456;

    private List<TbMessage> mMessageList = new LinkedList<>();
    private FriendModel mFriendModel = new FriendModel();

    @Override
    public int getItemViewType(int position) {
        Integer messageFromUserId = mMessageList.get(position).getMessageFromUserId();
        if (InfoRecord.getUserId().equals(messageFromUserId.toString())) {
            return TYPE_RIGHT;
        } else {
            return TYPE_LEFT;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_LEFT) {
            return new LeftViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_rv_chat_left, parent, false));
        } else {
            return new RightViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_rv_chat_right, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        TbMessage message = mMessageList.get(position);
        String messageContent = message.getMessageContent();
        String friendAvatarUrl = mFriendModel.getFriendAvatarUrl();
        if (holder instanceof LeftViewHolder) {

//            ((LeftViewHolder) holder).civ_avatar.post(new Runnable() {
//                @Override
//                public void run() {
//                    int avatarWidth = ((LeftViewHolder) holder).civ_avatar.getWidth();
//                    int rootWidth = ((LeftViewHolder) holder).rl_chat_root.getWidth();
//                    if (rootWidth > mWindowWith - avatarWidth) {
//                        rootWidth = mWindowWith - avatarWidth;
//                    }
//                    ViewGroup.LayoutParams layoutParams = ((LeftViewHolder) holder).rl_chat_root.getLayoutParams();
//                    layoutParams.width = rootWidth;
//                    ((LeftViewHolder) holder).rl_chat_root.setLayoutParams(layoutParams);
//                }
//            });

            if (StringUtils.isEmpty(friendAvatarUrl)) {
                Glide.with(holder.itemView).load(R.drawable.default_avatar).into(((LeftViewHolder) holder).civ_avatar);
            } else {
                Glide.with(holder.itemView).load(HttpConfig.PIC_ROOT + friendAvatarUrl).into(((LeftViewHolder) holder).civ_avatar);
            }
            ((LeftViewHolder) holder).tv_message.setText(messageContent);
        } else {


//            ((RightViewHolder) holder).civ_avatar.post(new Runnable() {
//                @Override
//                public void run() {
//                    int avatarWidth = ((RightViewHolder) holder).civ_avatar.getWidth();
//                    int rootWidth = ((RightViewHolder) holder).rl_chat_root.getWidth();
//                    if (rootWidth > mWindowWith - avatarWidth) {
//                        rootWidth = mWindowWith - avatarWidth;
//                    }
//                    ViewGroup.LayoutParams layoutParams = ((RightViewHolder) holder).rl_chat_root.getLayoutParams();
//                    layoutParams.width = rootWidth;
//                    ((RightViewHolder) holder).rl_chat_root.setLayoutParams(layoutParams);
//                }
//            });


            if (StringUtils.isEmpty(InfoRecord.getAvatarUrl())) {
                Glide.with(holder.itemView).load(R.drawable.default_avatar).into(((RightViewHolder) holder).civ_avatar);
            } else {
                Glide.with(holder.itemView).load(HttpConfig.PIC_ROOT + InfoRecord.getAvatarUrl()).into(((RightViewHolder) holder).civ_avatar);
            }
            ((RightViewHolder) holder).tv_message.setText(messageContent);
        }
    }

    @Override
    public int getItemCount() {
        return mMessageList.size();
    }

    public void clearAndNotifyMessageListChanged(List<TbMessage> messageList, FriendModel friendModel, LinearLayoutManager linearLayoutManager) {
        mFriendModel = friendModel;
        mMessageList.clear();
        mMessageList.addAll(messageList);
        notifyDataSetChanged();
        linearLayoutManager.scrollToPositionWithOffset(getItemCount() - 1, Integer.MIN_VALUE);
    }

    public void addMessage(TbMessage message) {
        mMessageList.add(message);
        notifyItemInserted(mMessageList.size() - 1);
    }

    public void addHistoryMessage(List<TbMessage> messageList) {
        mMessageList.addAll(0, messageList);
        notifyItemRangeInserted(0, messageList.size());
    }

    static class LeftViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView civ_avatar;
        private TextView tv_message;
        private RelativeLayout rl_chat_root;

        LeftViewHolder(@NonNull View itemView) {
            super(itemView);
            civ_avatar = itemView.findViewById(R.id.civ_avatar);
            tv_message = itemView.findViewById(R.id.tv_message);
            rl_chat_root = itemView.findViewById(R.id.rl_chat_root);

        }
    }

    static class RightViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView civ_avatar;
        private TextView tv_message;
        private RelativeLayout rl_chat_root;

        RightViewHolder(@NonNull View itemView) {
            super(itemView);
            civ_avatar = itemView.findViewById(R.id.civ_avatar);
            tv_message = itemView.findViewById(R.id.tv_message);
            rl_chat_root = itemView.findViewById(R.id.rl_chat_root);
        }
    }
}
